<template>
  <div class="mod--station">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template  slot="menuLeft">
        <el-button icon="el-icon-plus" type="primary" size="small" v-if="isAuth('admin:station:save')" @click="addOrUpdateHandle()">{{$t("crud.addTitle")}}</el-button>
      </template>
      <template slot-scope="scope" slot="addr">
        <span>{{scope.row.province}}{{scope.row.city}}{{scope.row.area}}{{scope.row.addr}}</span>
      </template>
      <template slot-scope="scope" slot="phone">
        <span
          v-if="scope.row.phonePrefix && scope.row.phonePrefix !== null"
        >{{scope.row.phonePrefix}}-</span>
        <span>{{scope.row.phone}}</span>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 4" size="small" type="info">{{$t("station.auditFailure")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 3" size="success">{{$t("station.underReview")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 2" type="warning">{{$t("components.platformOffline")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small">{{$t("station.business")}}</el-tag>
        <el-tag v-else size="small" type="danger">{{$t("station.close")}}</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          v-if="isAuth('admin:station:update')"
          @click="addOrUpdateHandle(scope.row.stationId)"
        >{{$t("crud.updateBtn")}}</el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('admin:station:changeAccountInfo')"
          @click="checkStationAccount(scope.row.stationId)"
        >{{scope.row.account?$t("admin.resetPwd"):$t("admin.createAcc")}}</el-button>
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('admin:station:auditApply') && scope.row.status > 1"
          @click="auditEventHandle(scope.row.stationId)"
        >{{scope.row.status === 3?$t("coupon.waitReview"):$t("admin.applyBus")}}</el-button>
        <el-button
          type="text"
          style="color:red"
          icon="el-icon-delete"
          size="small"
          v-if="isAuth('admin:station:delete')"
          @click="deleteHandle(scope.row.stationId)"
        >{{$t("text.delBtn")}}</el-button>
      </template>
    </avue-crud>
    <!-- 新增编辑弹窗-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 账号弹窗-->
    <station-account
      v-if="stationAccountVisible"
      ref="stationAccount"
      @refreshDataList="refreshChange"
    ></station-account>
    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/admin/station/getOfflineHandleEventByStatonId"
      applyUrl="/admin/station/auditApply"
      ref="offlineEvent"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import { tableOption } from '@/crud/admin/station'
import StationAccount from './station-account'
import AddOrUpdate from './station-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'

export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      dataListLoading: false,
      tableOption: tableOption,
      offlineEventHandleVisible: false,
      addOrUpdateVisible: false,
      stationAccountVisible: false
    }
  },
  components: {
    AddOrUpdate,
    StationAccount,
    OfflineEventHandle
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/admin/station/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('admin.isDeleOper') + '?', this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/admin/station/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 下线管理
     */
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    },
    /**
     * 账号管理
     */
    checkStationAccount (id) {
      this.stationAccountVisible = true
      this.$nextTick(() => {
        this.$refs.stationAccount.init(id)
      })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    }
  }
}
</script>
<style lang="scss">

</style>
